package com.titanrise.crm.dao;

import com.titanrise.crm.common.constants.SysConstant;
import com.titanrise.crm.domain.dto.RoleBelongDto;
import com.titanrise.crm.domain.dto.TradeDto;
import com.titanrise.crm.domain.dto.TradeTableDto;
import com.titanrise.crm.domain.dto.UserDto;
import com.titanrise.crm.domain.model.*;
import com.titanrise.crm.environment.annotation.Autowired;
import com.titanrise.crm.environment.annotation.Repository;
import com.titanrise.crm.mapper.*;

import java.util.List;

/**
 * @Description:
 * @Author: pront
 * @Time:2024-11-08 10:09
 */
@Repository
public class RootDao {

    //    FIXME 方法私有制
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private TradeMapper tradeMapper;

    @Autowired
    private SysLoginLogMapper sysLoginLogMapper;

    @Autowired
    private ClientSalesRelationshipsMapper clientSalesRelationshipsMapper;

    @Autowired
    private MessagesMapper messagesMapper;

    /**
     * 查询所有用户
     *
     * @return 所有的用户集合
     */
    public List<User> selectAllUsers() {
        return userMapper.selectAll();
    }

    public List<Role> selectAllRoles() {
        return roleMapper.selectAll();
    }

    public List<UserDto> selectAllMemberInfForRoot() {
        return userMapper.selectAllMemberInfForRoot();
    }

    public int updateRoleUsePasByUid(String username, String password, String uid) {
        return roleMapper.updateRoleInfoByUid(username, password, uid);
    }

    /**
     * 新增销售人员信息
     *
     * @param user 成员信息
     * @param role 角色信息
     * @return 受影响的记录行数
     */
    public int addSalesPersonInfo(User user, Role role) {
        return (userMapper.addUser(user) + roleMapper.addRole(role)) / 2;
    }

    public int deleteUserInfoByUid(String uid) {
        return (userMapper.deleteUserByID(uid) + roleMapper.deleteRoleByUid(uid)) / 2;
    }

    /**
     * 根据交易状态顺序排序
     * @return
     */
    public List<TradeDto> selectAllTradesForRoot() {
        return tradeMapper.selectAllDto();
    }

    public int deleteTradeInfoByTradeId(String tradeId) {
        return tradeMapper.deleteById(tradeId);
    }

    /**
     * 查看系统登录日志
     * @param timeLine 时间限制
     * @param page 当前页码
     * @return 日志集合
     */
    public List<SysLoginLog> selectLogInfo(int timeLine, int page) {
//        默认页码为5
        int offset = (page - 1) * SysConstant.PAGE_SIZE;
        return sysLoginLogMapper.selectLogInfo(timeLine, offset);
    }

    public int selectLogInfoCount(int timeLine) {
        return sysLoginLogMapper.selectLogInfoCount(timeLine);
    }

    public List<ClientSalesRelationships> showSalespersonOfClient() {
        return clientSalesRelationshipsMapper.selectAll();
    }

    public String selectNameByUid(String salesId) {
        return roleMapper.selectNameByUid(salesId);
    }

    public List<Role> selectSalesperson() {
        return roleMapper.selectAllSalesperson();
    }

    public int sendMessage(Messages message) {
        return messagesMapper.insertOne(message);
    }

    public int selectRoleByUid(String uid) {
        return roleMapper.selectExistByUid(uid);
    }

    public List<RoleBelongDto> getNotBelongClient() {
        return roleMapper.selectNotBelongClient();
    }

    public int updateNotBelongClient(String clientUid) {
        return roleMapper.updateNotBelongClient(clientUid);
    }

    public int insertOneRelationship(ClientSalesRelationships clientSalesRelationships) {
        return clientSalesRelationshipsMapper.insertOne(clientSalesRelationships);
    }

    public List<TradeTableDto>  selectTradeTable() {
        return tradeMapper.selectTradeTable();
    }

    public List<User> selectAllRelativeUserForSalesId(String uid) {
        return clientSalesRelationshipsMapper.selectAllRelativeUserForSalesId(uid);
    }

    public int updateClientNonBelongById(String uid) {
        return roleMapper.updateClientNonBelongById(uid);
    }

    public int deleteRelationshipsForSalesId(String salesUid) {
        return clientSalesRelationshipsMapper.deleteRelationshipsForSalesId(salesUid);
    }
}
